package com.gopay.common.security.cacert.dao.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.cust.CustCaCertConstants;
import com.gopay.common.domain.user.CauthCaCert;
import com.gopay.common.security.cacert.dao.UkConsoleCaCertQueryDao;
import com.gopay.common.util.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 控制台硬证书公共查询dao实现类 @UkConsoleCaCertQueryDaoImpl.java
 * @author zhuliang  @2017年10月10日
 * 下午2:39:20  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("ukConsoleCaCertQueryDao")
public class UkConsoleCaCertQueryDaoImpl extends BaseDAOImpl<CauthCaCert, Serializable> implements UkConsoleCaCertQueryDao {

    @Override
    public CauthCaCert getCauthCaCertByOperId(String operId) {
        
        if (StringUtils.isEmpty(operId)) {
            return null;
        }
        
        Map<String, Object> param = new HashMap<>();
        param.put("operId", operId);
        List<CauthCaCert> certList = findByHql("from CauthCaCert t where t.operId =:operId order by t.id desc ", param);
        
        if (certList != null && certList.size() > 0) {
            return certList.get(0);
        }
        return null;
    }


	@Override
    public List<CauthCaCert> getLimitCauthCaCert() {
		/**
		 * 查询出状态为启用，并且已经过期的数据
		 */
		String hql = "from CustCaCert where stat= :stat and "
				   + "TO_CHAR(VALID_TO,'yyyy-MM-dd') <= :currentDate ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("stat", CustCaCertConstants.CUST_CA_CERT_STAT_ACTIVE);
		param.put("currentDate", DateUtils.format(new Date(), "yyyy-MM-dd"));

		List<CauthCaCert> list = findByHql(hql, param);
		return list;
	}


	/**
	 * 根据证书主键查询证书信息
	 *
	 */
	@Override
    public CauthCaCert getCauthCaCertById(Serializable certId) {
		CauthCaCert cert = find(CauthCaCert.class, certId);
        return cert;
	}

	/**
	 * 根据operId和证书状态查询证书列表
	 */
	@Override
    public List<CauthCaCert> getCauthCaCertListByOperId(String operId, String stat) {
		String hql = "from CauthCaCert where stat= :stat and "
				+ " operId =:operId order by id desc ";
		Map<String, Object> param = new HashMap<>();
		param.put("stat", stat);
		param.put("operId", operId);
		
		List<CauthCaCert> list = findByHql(hql, param);
		return list;
	}

	@Override
    public List<CauthCaCert> getNeedUpdateCauthCaCert(String operId){
		String hql = "from CauthCaCert where stat= '"+  CustCaCertConstants.CUST_CA_CERT_STAT_ACTIVE +"' and "
				+ " operId = :operId and validTo <= :oneMonthAgo ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("operId", operId);
		param.put("oneMonthAgo", DateUtils.add(new Date(), Calendar.MONTH, -1));
		
		List<CauthCaCert> list = findByHql(hql, param);
		return list;
	}

	/**
     * 根据操作员ID和序列号获取证书
     */
	@Override
    public CauthCaCert getByUserIdAndSN(String operId, String sn){
	    String hql = "from CauthCaCert where sn= :sn and "
                + " operId =:operId ";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sn", sn);
        param.put("operId", operId);

        List<CauthCaCert> list = findByHql(hql, param);

        return list!=null&&list.size()>0?list.get(0):null;
    }


	@Override
    public CauthCaCert getExceedByUserId(String operId){

		String hql = "from CauthCaCert where operId= :operId and "
				+ "TO_CHAR(VALID_TO,'yyyy-MM-dd') <= :currentDate and stat= '01' order by id desc ";
		Map<String, Object> param = new HashMap<>();
		param.put("operId", operId);
		param.put("currentDate", DateUtils.format(new Date(), "yyyy-MM-dd"));

		List<CauthCaCert> certList = findByHql(hql, param);

		if (certList != null && certList.size() > 0) {
			return certList.get(0);
		}

		return null;
	}

	@Override
	public Map<String,Object> isUkCertExpireTip(String operId) {

        String sql ="from CauthCaCert t where trunc(t.validTo) - trunc(sysdate)>0 and trunc(t.validTo)- trunc(sysdate)<=30 and t.stat='01' and t.operId='"+operId+"'";
        Map<String, Object> result = new HashMap<>();
		List<CauthCaCert> list = findByHql(sql);
		if(null!=list &&list.size()>0){
			CauthCaCert CauthCaCert = list.get(0);
		    Date date = CauthCaCert.getValidTo();

			Calendar c = Calendar.getInstance();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
			String validTo = sf.format(date);

			result.put("isUkSwitch", true);
			result.put("ukValidTo", validTo);
        }else{
			result.put("isUkSwitch", false);
		}
		return result;
	}

}
